package cn.lyq.card.ui.agent

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.AgentInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.phone.PhoneCallFg
import cn.lyq.card.ui.statistics.DevProxyDetailActivity
import cn.lyq.card.utils.getVisibility
import cn.lyq.card.utils.loadUrl
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_agent_detail.*


class AgentDetailActivity : BaseActivity() {

    companion object {

        private const val KEY_TYPE = "TYPE"
        private const val KEY_ID = "ID"

        /**
         * 0日、1月、2全部
         */
        @JvmStatic
        fun start(context: Context, type: Int, id: Int) {
            val starter = Intent(context, AgentDetailActivity::class.java)
                .putExtra(KEY_TYPE, type)
                .putExtra(KEY_ID, id)
            context.startActivity(starter)
        }
    }

    private val mModel by lazy {
        getVM(AgentListVM::class.java)
    }
    private val type by lazy {
        intent.getIntExtra(KEY_TYPE, 0)
    }
    private val id by lazy {
        intent.getIntExtra(KEY_ID, 0)
    }


    override fun getLayoutId(): Int {
        return R.layout.act_agent_detail
    }

    override fun initView() {
        super.initView()

        tvPhoneHint.setClick {
            PhoneCallFg.newInstance(tvPhone.text.toString()).show(supportFragmentManager)
        }
        val amountVisibility = getVisibility(type == 2)
        val detailVisibility = getVisibility(type != 2)

        tvStock.visibility = amountVisibility
        tvStockHint.visibility = amountVisibility

        tvActive.visibility = detailVisibility
        tvActiveHint.visibility = detailVisibility
        tvActiveSplit.visibility = detailVisibility
        tvUnActive.visibility = detailVisibility
        tvUnActiveHint.visibility = detailVisibility
        tvCheckDetailActive.visibility = detailVisibility
        tvCheckDetailUnActive.visibility = detailVisibility
    }

    override fun initListener() {
        super.initListener()
        refreshLayout.setOnRefreshListener {
            getAgentDetail()
        }
        tvCheckDetailUnActive.setClick {
            DevProxyDetailActivity.start(this, id, 1, isAgent = true)
        }
        tvCheckDetailActive.setClick {
            DevProxyDetailActivity.start(this, id, 0, isAgent = true)
        }
    }

    override fun initObserver() {
        super.initObserver()
        getAgentDetail()
    }

    private fun getAgentDetail() {
        mModel.getAgentDetail(id).observe(this, Observer {
            when (it?.status) {
                Status.LOADING -> refreshLayout.isRefreshing = true
                Status.SUCCESS -> setData(it.requireData())
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    refreshLayout.isRefreshing = false
                }
            }
        })
    }

    private fun setData(info: AgentInfo) {
        setMyTitle(info.agentName ?: getString(R.string.str_agent_detail))
        imgAvatar.loadUrl(info.userIcon)
        tvName.text = info.trueName ?: ""
        tvPhone.text = info.mobile ?: ""
        if (type == 2) {
            tvActive.text = info.actPosNumber.toString()
            tvUnActive.text = info.notActPosNumber.toString()
        } else {
            tvStock.text = info.posNumber.toString()
        }
        tvOpenTime.text = info.rgTime
        refreshLayout.isRefreshing = false
    }
}